package com.bright.tactiviti.process.deployment;


import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class ProcessDeploymentNote {
    @Autowired
    private RepositoryService repositoryService;

    /**
     * 流程部署 ---- 使用流程文件资源部署
     * 操作对应数据表
     * ACT_GE_BYTEARRAY
     * ACT_RE_DEPLOYMENT
     * ACT_RE_PROCDEF
     */

    @Test
    public void processDeploymentUseClasspathResource(){
        //创建流程部署工具
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        //bpmn 文件路径
       // String bpmnResourcePath = "processes/note/bpmn/LeaveProcess.bpmn";
        //String bpmnResourcePath = "processes/note/group/groupProcess.bpmn";
        String bpmnResourcePath = "processes/note/file/fileProcess.bpmn";
        //png 文件路径
       // String pngResourcePath = "processes/note/bpmn/LeaveProcess.png";
//        String pngResourcePath = "processes/note/group/groupProcess.png";
        String pngResourcePath = "processes/note/file/fileProcess.png";
        //流程名称
        String processName = "文件审批流程";

        /**
         * 流程部署，其中就需要两个文件对应的路径
         * 同时我们在部署的时候可以给这个流程起一个名字
         * 部署后返回一个部署对象
         */
        Deployment deployment = deploymentBuilder.name(processName).addClasspathResource(bpmnResourcePath).addClasspathResource(pngResourcePath).deploy();

        System.out.println("流程部署成功，流程部署ID：" + deployment.getId());
    }

    /**
     * 查询部署流程
     * 操作表
     * ACT_RE_DEPLOYMENT
     */
    @Test
    public void processDeploymentQuery(){
        //获取流程部署查看器
        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();

        List<Deployment> deployments =  deploymentQuery
//                //匹配查询条件
//                /*流程部署ID String*/
//                .deploymentId("1")
//                /*流程部署的name String*/
//                .deploymentName("")
//                /*流程部署的name 模糊匹配*/
//                .deploymentNameLike("")
//
//                //排序规则
//                /*流程部署id 升序排序*/
//                .orderByDeploymentId().asc()
//                /*流程部署Name 降序*/
//                .orderByDeploymentName().desc()
//                /*流程部署Time 升序*/
//                .orderByDeploymenTime().asc()
                .list();

        // 循环结果集
        deployments.forEach(deployment -> {
            System.out.println("流程部署 ID: " + deployment.getId());
            System.out.println("流程部署 Name: " + deployment.getName());
           // System.out.println("流程部署 Time: " + DateUtil.dateTime2String(deployment.getDeploymentTime()));
            System.out.println(" = = = = = = = = = = = = = = = = = = = = = = = = = ");
        });

    }
    /***
     * 删除流程部署
     * 删除流程部署的同时，流程定义，正在执行的流程实例，该流程的历史记录都会被删除
     *
     * 操作的表
     * ACT_GE_BYTEARRAY
     * ACT_RE_*
     * ACT_RU_*
     * ACT_HI_*
     */
    @Test
    public void processDeploymentDelete(){
        //需要删除的流程部署ID
        String deploymentId = "";

        /*
         * 第一个参数就是需要删除的部署 ID
         * 第二个参数是代表当前如果还存在尚未执行完毕的程序，则是否强制删除
         *      true：即使存在为执行完毕的流程，扔强制删除
         *      false：存在尚未删除的流程，则不删除
         * */
        repositoryService.deleteDeployment(deploymentId,true);
        System.out.println("删除成功");
    }
}
